package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavors;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDishes;
import com.itheima.jsonresult.JsonResult;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.DishFlavorsService;
import com.itheima.service.SetmealDishesService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linying
 * @since 2022-10-13
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishesService setmealDishesService;

    /**
     * 新增套餐
     * @param setmeal
     * @return
     */
    @Override
    public JsonResult add(Setmeal setmeal) {
        // 根据username查询是否存在
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", setmeal.getName());

        // 查询结果为空，不存在
        if ((getOne(wrapper) == null)) {
            // 先添加dish
            save(setmeal);
            // 循环遍历添加flavors
            for (SetmealDishes s : setmeal.getSetmealDishes()) {
                SetmealDishes setmealDishes = new SetmealDishes();
                setmealDishes.setSetmealId(setmeal.getId());
                setmealDishes.setName(s.getName());
                setmealDishes.setCopies(s.getCopies());
                setmealDishes.setDishId(s.getDishId());
                setmealDishes.setPrice(s.getPrice());
                setmealDishesService.save(setmealDishes);
            }
            return new JsonResult(1, null, "新增套餐成功",null);
        } else {
            return new JsonResult(0, null, setmeal.getName() + "已存在",null);
        }
    }

    /**
     *修改套餐
     * @param setmeal
     * @return
     */
    @Override
    public JsonResult put(Setmeal setmeal) {
        // 添加查询的条件name
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", setmeal.getName());
        // 根据name修改dish表
        update(setmeal, wrapper);
        // 获取flavors的数组值
        for (SetmealDishes s : setmeal.getSetmealDishes()) {
            SetmealDishes setmealdishes = new SetmealDishes();
            setmealdishes.setName(s.getName());
            setmealdishes.setCopies(s.getCopies());
            setmealdishes.setDishId(s.getDishId());
            setmealdishes.setPrice(s.getPrice());
            // 修改flavors的值
            QueryWrapper swrapper = new QueryWrapper();
            wrapper.eq("setmeal_id", setmeal.getId());
            setmealDishesService.remove(wrapper);
            setmealDishesService.save(setmealdishes);
        }
        return JsonResult.success("修改套餐成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @Override
    public JsonResult delete(String ids) {
        //分割获取数组
        String[] split = ids.split(",");
        //数组转集合
        List<String> list = Arrays.asList(split);

        removeByIds(list);
        return JsonResult.success("删除成功");
    }

    /**
     * 套餐分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult getPage(String name, Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        // 姓名可选非空条件判断
        if (!StrUtil.isBlank(name)) {
            wrapper.eq("name", name);
        }
        // 调用分页查询方法
        Page jsonpage = (Page) page(new Page<>(page, pageSize), wrapper);

        return JsonResult.success(jsonpage);
    }

    /**
     * 套餐上架/下架
     * @param status
     * @param ids
     * @return
     */
    @Override
    public JsonResult status(String status, String ids) {
        //切割ids字符串获取数组
        String[] split = ids.split(",");
        //数组转集合
        List<String> id = Arrays.asList(split);

        //遍历id集合
        for (String s : id) {
            //根据id获取dish对象
            Setmeal setmeal = getById(s);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("id", setmeal.getId());

            //判断status的值
            if (setmeal.getStatus()==0) {
                setmeal.setStatus(1);
                //根据id修改status的值
                update(setmeal, wrapper);
            } else {
                setmeal.setStatus(0);
                update(setmeal, wrapper);
            }
        }
        return new JsonResult(1, null, "操作成功",null);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public JsonResult getSetmealById(String id) {
        // 1、查询套餐表
        Setmeal setmeal = getById(id);
        // 2、查询套餐菜品表
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);

        List<SetmealDishes> list = setmealDishesService.list(wrapper);

        // 3、重新装到套餐类里
        setmeal.setSetmealDishes(list);

        return new JsonResult(1,null,setmeal,null);
    }

    /**
     * 套餐列表
     * @param categoryId
     * @param status
     * @return
     */
    @Cacheable(value = "dish", key = "'dishList'+#categoryId")
    @Override
    public JsonResult setmealList(String categoryId, String status) {
        //根据分类id和状态查询套餐列表
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id", categoryId);
        wrapper.eq("status", status);

        List setmeal = list(wrapper);
        return JsonResult.success(setmeal);
    }
}
